import ctypes  

Libairshm = "libairshm.so"
Libbcmshm = "libbcmshm.so"
Libarrayshm  =   "libbcmshm.so"
Libclustershm    =   "libbcmshm.so"
Libesmushm    =    "libbcmshm.so"
Libemushm    =    "libemushm.so"
Libfireshm   =     "libfireshm.so"
Libioshm     =   "libesmudido.so"
Libioshm_io   =      "libioshm.so"
Libliqcoolshm   =     "libliqcoolshm.so"




class Cluster:
    def __init__(self,array,cluster):
        try:  
            self.lib = ctypes.CDLL("libbcmshm.so")  
        except OSError as e:  
            raise ValueError(f"无法加载库 libbcmshm.so: {e}")  
        
        # 设置 get_cluster_handle 函数的参数和返回类型  
        self.lib.get_cluster_handle.argtypes = [ctypes.c_int, ctypes.c_int]  
        self.lib.get_cluster_handle.restype = ctypes.c_void_p  
  
        # 调用 get_cluster_handle 并检查返回值  
        self.handle = self.lib.get_cluster_handle(array, cluster)  
        if not self.handle:  
            raise ValueError("get_cluster_handle 返回无效句柄")  

    def set_cluster_di_data(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

    def get_cluster_di_data(self,func_name,args):
        # 获取函数句柄并设置参数和返回类型  
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def get_cluster_ai_data_int(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def set_cluster_ai_data_int(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def get_cluster_ai_data_float(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_float)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def set_cluster_ai_data_float(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_float]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def get_cluster_param_data_int(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(value),ctypes.byref(flag))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

    def set_cluster_param_data_int(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, value,flag)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def get_cluster_param_data_float(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(value),ctypes.byref(flag))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def set_cluster_param_data_float(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_float,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, value,flag)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

class Array:
    def __init__(self,devno):
        try:  
            self.lib = ctypes.CDLL("libbcmshm.so")  
        except OSError as e:  
            raise ValueError(f"无法加载库 libbcmshm.so: {e}")  
        
        # 设置 get_array_handle 函数的参数和返回类型  
        self.lib.get_array_handle.argtypes = [ctypes.c_int]  
        self.lib.get_array_handle.restype = ctypes.c_void_p  
  
        # 调用 get_array_handle 并检查返回值  
        self.handle = self.lib.get_array_handle(devno)  
        if not self.handle:  
            raise ValueError("get_cluster_handle 返回无效句柄")  
    def set_array_di_data(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

    def get_array_di_data(self,func_name,args):
        # 获取函数句柄并设置参数和返回类型  
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def get_array_ai_data_int(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def set_array_ai_data_int(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def get_array_ai_data_float(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_float)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def set_array_ai_data_float(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_float]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def get_array_param_data_int(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(value),ctypes.byref(flag))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

    def set_array_param_data_int(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, value,flag)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def get_array_param_data_float(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(value),ctypes.byref(flag))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def set_array_param_data_float(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_float,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, value,flag)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

class Esmu:
    def __init__(self):
        try:  
            self.lib = ctypes.CDLL("libbcmshm.so")  
        except OSError as e:  
            raise ValueError(f"无法加载库 libbcmshm.so: {e}")  
        
        # 设置 get_array_handle 函数的参数和返回类型  
        # self.lib.get_array_handle.argtypes = []  
        self.lib.get_esmu_handle.restype = ctypes.c_void_p  
  
        # 调用 get_array_handle 并检查返回值  
        self.handle = self.lib.get_esmu_handle()  
        if not self.handle:  
            raise ValueError("get_cluster_handle 返回无效句柄")  
    def set_esmu_di_data(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

    def get_esmu_di_data(self,func_name,args):
        # 获取函数句柄并设置参数和返回类型  
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def get_esmu_ai_data_int(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def set_esmu_ai_data_int(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def get_esmu_ai_data_float(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_float)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(args))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def set_esmu_ai_data_float(self,func_name,args):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_float]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, args)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    
    def get_esmu_param_data_int(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(value),ctypes.byref(flag))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  

    def set_esmu_param_data_int(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_int,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, value,flag)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def get_esmu_param_data_float(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_int)]  
        func.restype = ctypes.c_int  
  
        result = func(self.handle, ctypes.byref(value),ctypes.byref(flag))  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    def set_esmu_param_data_float(self,func_name,value,flag):
        func = getattr(self.lib, func_name)  
        func.argtypes = [ctypes.c_void_p,ctypes.c_float,ctypes.c_int]  
        func.restype = ctypes.c_int  
  
        # 调用函数并返回结果  
        result = func(self.handle, value,flag)  
        if result < 0: 
            raise ValueError(f"{func_name} 调用失败，返回 {result}")  
        return result  
    


def main():
     print("hello")
     Cluster_0_0 = Cluster(0,0)
     value = ctypes.c_int()  
     Cluster_0_0.get_cluster_di_data("cluster_di_get_clucelsoc_low2",value)
     Cluster_0_0.set_cluster_di_data("cluster_di_set_clulsoc_low2",0)
     print(value)
  
if __name__ == '__main__':  
    main()
